﻿Imports System.Data.SqlClient
Imports System.IO
Imports System.Data
Public Class ViewAdmins
    Inherits System.Web.UI.Page

    Dim con As New DBConnect

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'check if user signed in if not going back to login page 
        If Not Session.Item("UserIn").Equals("yes") Then
            Response.Redirect("default.aspx")
        End If
        lblMsg.Visible = False
        'connecting to database 
        Dim SqlConnectString As String = con.SqlConnectString
        Dim myConnection As New SqlConnection(SqlConnectString)
        Dim myCommand As New SqlCommand()
        ' Dim result As Integer
        myCommand.Connection = myConnection
        'Set the SQLCommand object's properties for executing a Stored Procedure
        myCommand.CommandType = CommandType.StoredProcedure
        myCommand.CommandText = "SelectAdmins"       'Name of the Stored Procedure to execute

        Try
            myConnection.Open()
            gvAdminList.EmptyDataText = "No Records Found"

            Dim i As Integer = 0
            Dim r As Integer = 0
            gvAdminList.DataSource = myCommand.ExecuteReader()
            gvAdminList.DataBind()
            While i < gvAdminList.Rows.Count
                If gvAdminList.Rows(r).Cells(2).Text.Equals("2") Then
                    gvAdminList.Rows(r).Cells(2).Text = "Regular Administrator"
                Else
                    gvAdminList.Rows(r).Cells(2).Text = "Super Administrator"
                End If
                i = i + 1
                r = r + 1
            End While
        


        Catch ex As Exception
            Throw ex
        Finally
            myConnection.Close()
            myConnection.Dispose()
        End Try


    End Sub

    'Deleting Admin From Database fired up when user clicks delete
    Protected Sub gvAdminList_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles gvAdminList.RowDeleting

        Dim rowIndex As Integer = e.RowIndex
        Dim id As Integer = CInt(gvAdminList.DataKeys(rowIndex).Value)
        Dim NetId As String = CStr(gvAdminList.Rows(rowIndex).Cells(0).Text)
        Dim result As Integer
        'connecting to database 
        Dim SqlConnectString As String = con.SqlConnectString
        Dim myConnection As New SqlConnection(SqlConnectString)
        Dim myCommand As New SqlCommand()
        myCommand.Connection = myConnection

        'Set the SQLCommand object's properties for executing a Stored Procedure
        myCommand.CommandType = CommandType.StoredProcedure
        myCommand.CommandText = "RemoveAdmin"       'Name of the Stored Procedure to execute

        Dim inputParameter As New SqlParameter("@ID", id)
        inputParameter.Direction = ParameterDirection.Input
        myCommand.Parameters.Add(inputParameter)

        Dim returnParameter As New SqlParameter("@new_identity", 0)
        returnParameter.Direction = ParameterDirection.Output
        myCommand.Parameters.Add(returnParameter)
        Try
            myConnection.Open()
            myCommand.ExecuteScalar()

        Catch ex As Exception
            Throw ex
        Finally
            If Not IsDBNull(myCommand.Parameters("@new_identity").Value) Then
                result = CType(myCommand.Parameters("@new_identity").Value, Integer)

                If result = 0 Then
                    lblMsg.Visible = True
                    lblMsg.Text = "Success! User ID: " + NetId + " has been removed."
                    bindGridView()
                    Dim i As Integer = 0
                    Dim r As Integer = 0
                    While i < gvAdminList.Rows.Count
                        If gvAdminList.Rows(r).Cells(2).Text.Equals("2") Then
                            gvAdminList.Rows(r).Cells(2).Text = "Regular Administrator"
                        Else
                            gvAdminList.Rows(r).Cells(2).Text = "Super Administrator"
                        End If
                        i = i + 1
                        r = r + 1
                    End While
                Else
                    lblMsg.Visible = True
                    lblMsg.Text = "An error has occured. Try refreshing page and try again."
                End If
            Else
                bindGridView()
                Dim i As Integer = 0
                Dim r As Integer = 0
                While i < gvAdminList.Rows.Count
                    If gvAdminList.Rows(r).Cells(2).Text.Equals("2") Then
                        gvAdminList.Rows(r).Cells(2).Text = "Regular Administrator"
                    Else
                        gvAdminList.Rows(r).Cells(2).Text = "Super Administrator"
                    End If
                    i = i + 1
                    r = r + 1
                End While
                lblMsg.Visible = True
                lblMsg.Text = "Success! User ID: " + NetId + " has been removed."
            End If
            myConnection.Close()
            myConnection.Dispose()
            removeAdminFromDepartment(NetId.Trim)
        End Try
    End Sub

    Private Sub removeAdminFromDepartment(ByVal netIDIn As String)
        Dim NetId As String = netIDIn.Trim

        'connecting to database 
        Dim SqlConnectString As String = con.SqlConnectString
        Dim myConnection As New SqlConnection(SqlConnectString)
        Dim myCommand As New SqlCommand()
        myCommand.Connection = myConnection

        'Set the SQLCommand object's properties for executing a Stored Procedure
        myCommand.CommandType = CommandType.StoredProcedure
        myCommand.CommandText = "RemoveAdminFromDepartments"       'Name of the Stored Procedure to execute

        Dim inputParameter As New SqlParameter("@accessNetID", NetId)
        inputParameter.Direction = ParameterDirection.Input
        myCommand.Parameters.Add(inputParameter)
        Try
            myConnection.Open()
            myCommand.ExecuteScalar()

        Catch ex As Exception
            Throw ex
        Finally

            myConnection.Close()
            myConnection.Dispose()
        End Try
    End Sub
    Protected Sub bindGridView()
        'connecting to database 
        Dim SqlConnectString As String = con.SqlConnectString
        Dim myConnection As New SqlConnection(SqlConnectString)
        Dim myCommand As New SqlCommand()
        ' Dim result As Integer
        myCommand.Connection = myConnection
        'Set the SQLCommand object's properties for executing a Stored Procedure
        myCommand.CommandType = CommandType.StoredProcedure
        myCommand.CommandText = "SelectAdmins"       'Name of the Stored Procedure to execute

        Try
            myConnection.Open()
            gvAdminList.EmptyDataText = "No Records Found"
            gvAdminList.DataSource = myCommand.ExecuteReader()

            gvAdminList.DataBind()


        Catch ex As Exception
            Throw ex
        Finally
            myConnection.Close()
            myConnection.Dispose()
        End Try
    End Sub
End Class